﻿/*************************************************************************
 * Created by OhZee Creative, LLC
 * 2013
 * This code is licensed under Microsoft Public License (Ms-PL)
 * http://opensource.org/licenses/ms-pl
 * http://ozframework.codeplex.com/
 *************************************************************************/

namespace OhZeeCreative.Windows8.UnitTests
{
    using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
    using OhZeeCreative.Helpers;
    using System;

    [TestClass]
    public class MathHelperTest
    {
		[TestMethod]
		public void TestAdjustedAngle()
		{
			const int accuracy = 10; // digits

			double angle;

			double radians30 = MathHelper.Radians(30);
			Assert.AreEqual(Math.PI / 6.0, radians30);

			double radians45 = MathHelper.Radians(45);
			Assert.AreEqual(Math.PI * 0.25, radians45);

			double radians90 = MathHelper.Radians(90);
			Assert.AreEqual(Math.PI * 0.5, radians90);

			double radians135 = MathHelper.Radians(135);
			Assert.AreEqual(Math.PI * 0.75, radians135);

			double radians225 = MathHelper.Radians(225);
			Assert.AreEqual(Math.PI * 1.25, radians225);

			double radians270 = MathHelper.Radians(270);
			Assert.AreEqual(Math.PI * 1.5, radians270);

			double radians315 = MathHelper.Radians(315);
			Assert.AreEqual(Math.PI * 1.75, radians315);

			// Test where the offset is right at the tip
			angle = MathHelper.AdjustedAngle(radians30, 1, Math.Cos(radians30), Math.Sin(radians30));
			Assert.AreEqual(radians30, angle);

			// Test with a = 30 degrees, x offset < rcosa,  no y offset
			angle = MathHelper.AdjustedAngle(radians30, 2 * Math.Sqrt(2), Math.Sqrt(6) - Math.Sqrt(2), 0);
			Assert.AreEqual(Math.Round(radians45, accuracy), Math.Round(angle, accuracy));

			// Test with a = 30 degrees, x offset = rcosa, no y offset
			angle = MathHelper.AdjustedAngle(radians30, 1, Math.Cos(radians30), 0);
			Assert.AreEqual(radians90, angle);

			// Test with a = 30 degrees, x offset = rcosa, y offset > 0 (doesn't matter what it is)
			angle = MathHelper.AdjustedAngle(radians30, 1, Math.Cos(radians30), 1);
			Assert.AreEqual(radians270, angle);

			// Test with a = 45 degrees, x offset = 2*rcosa, no y offset
			angle = MathHelper.AdjustedAngle(radians45, 1, 2 * Math.Cos(radians45), 0);
			Assert.AreEqual(radians135, angle);

			// Test with a = 45 degrees, x offset = 2*rcosa, y = 2 * rsina
			angle = MathHelper.AdjustedAngle(radians45, 1, 2 * Math.Cos(radians45), 2 * Math.Sin(radians45));
			Assert.AreEqual(radians225, angle);

			///////////////////////////////

			// Test with a = 140 degrees, x = r, y = 0
			angle = MathHelper.AdjustedAngle(MathHelper.Radians(140), 1, 1, 0);
			Assert.AreEqual(MathHelper.Radians(160), angle);

			angle = MathHelper.AdjustedAngle(MathHelper.Radians(150), 2 * Math.Sqrt(2), -(Math.Sqrt(6) - Math.Sqrt(2)), 0);
			Assert.AreEqual(Math.Round(radians135, accuracy), Math.Round(angle, accuracy));

			angle = MathHelper.AdjustedAngle(MathHelper.Radians(360 + 150), 2 * Math.Sqrt(2), -(Math.Sqrt(6) - Math.Sqrt(2)), 0);
			Assert.AreEqual(Math.Round(radians135, accuracy), Math.Round(angle, accuracy));

			///////////////////////////////
			angle = MathHelper.AdjustedAngle(radians225, 1, -1, 0);
			Assert.AreEqual(Math.Round(MathHelper.Radians(360 - ((180 - 45) / 2.0)), accuracy), Math.Round(angle, accuracy));

			angle = MathHelper.AdjustedAngle(radians315, 1, 1, 0);
			Assert.AreEqual(Math.Round(MathHelper.Radians(180 + ((180 - 45) / 2.0)), accuracy), Math.Round(angle, accuracy));

			angle = MathHelper.AdjustedAngle(radians315, 1, 0, 1);
			Assert.AreEqual(Math.Round(MathHelper.Radians(270 + ((180 - 135) / 2.0)), accuracy), Math.Round(angle, accuracy));
		}
    }
}
